﻿using BLL;
using MODEL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Loginter
{
    public partial class UpdateTruckList : System.Web.UI.Page
    {
        //调用BLL层的车辆信息
        TruckBLL truckbll = new TruckBLL();
        //调用BLL层的车队信息
        TruckTeamBLL truckteam = new TruckTeamBLL();
        //调用BLL层车辆信息
        SelectTruckBLL truck = new SelectTruckBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int id = Convert.ToInt32(Request.QueryString["id"]);
                if (id == 0)
                {
                    Response.Redirect("TruckInfoList.aspx");
                }
                BindTruck();
                BindtruckList();
                BindcheliangList();
            }
        }
        /// <summary>
        /// 绑定所属车队名称
        /// </summary>
        private void BindtruckList()
        {
            DataTable table = truckteam.GetTruckList();
            drop_TruckName.DataSource = table;
            drop_TruckName.DataTextField = "TeamName";
            drop_TruckName.DataValueField = "TeamID";
            drop_TruckName.DataBind();
            drop_TruckName.Items.Insert(0, new ListItem("不限", "0"));
        }
        /// <summary>
        /// 绑定车辆类型
        /// </summary>
        private void BindcheliangList()
        {
            DataTable table = truck.GetTruckList();
            drop_TruckType.DataSource = table;
            drop_TruckType.DataTextField = "Type";
            drop_TruckType.DataValueField = "Type";
            drop_TruckType.DataBind();
            drop_TruckType.Items.Insert(0, new ListItem("--全部--", "0"));
        }

        /// <summary>
        /// 修改按钮，修改车辆信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_TruckAdd_Click(object sender, EventArgs e)
        {
            //车辆号码
            string numer = txt_TruckNumber.Text.Trim();
            //车队名称
            string truck = drop_TruckName.SelectedValue;
            //车辆类型
            string type = drop_TruckType.SelectedValue;
            //购车时间
            string cart = txt_BuyTim.Value.Trim();
            //车身长度
            string leng = txt_Length.Text.Trim();
            //车辆吨位
            string tonn = txt_Tonngae.Text.Trim();
            //工作状态
            string state = drop_State.SelectedValue;
            //修改时间
            string time = DateTime.Now.ToShortDateString();
            //车辆id
            int id = Convert.ToInt32(Request.QueryString["id"]);

            TruckMODEL trmo = new TruckMODEL();
            trmo.Number = numer;
            trmo.TeamName = truck;
            trmo.Type = type;
            trmo.State = drop_State.SelectedValue;
            trmo.BuyDate = Convert.ToDateTime(cart);
            trmo.Length = leng;
            trmo.Tonngae = Convert.ToInt32(tonn);
            trmo.AlterTime = Convert.ToDateTime(time);
            trmo.TruckID = id;

            int result = truckbll.UpdateTruck(trmo);
            if (result > 0)
            {
                //跳转页面到用户信息
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('修改成功！');location.href='TruckInfoList.aspx';</script>");
            }
            else
            {
                lab_Add.Text = "修改失败！";
            }
        }
        /// <summary>
        /// 绑定车辆信息
        /// </summary>
        private void BindTruck()
        {
            //车辆id
            int id = Convert.ToInt32(Request.QueryString["id"]);
            TruckMODEL tm = truckbll.GetUpdateTruckID(id);
            if (tm != null)
            {
                txt_TruckNumber.Text = tm.Number;
                drop_TruckName.SelectedValue = tm.TeamName;
                drop_TruckType.SelectedValue = tm.Type;
                txt_BuyTim.Value = tm.BuyDate.ToString();
                txt_Length.Text = tm.Length;
                txt_Tonngae.Text = tm.Tonngae.ToString();
                drop_State.SelectedValue = tm.State;
            }
        }
    }
}